package com.lantu.common.vo;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
//@ApiModel(value = "全局统一返回结果")
public class RResult<T> {

    //    @ApiModelProperty(value = "返回码")
    private Integer code;

    //    @ApiModelProperty(value = "返回消息")
    private String message;

    //    @ApiModelProperty(value = "返回数据")
    private T data;

    public RResult(){}

    // 返回数据
    protected static <T> RResult<T> build(T data) {
        RResult<T> RResult = new RResult<T>();
        if (data != null)
            RResult.setData(data);
        return RResult;
    }

    public static <T> RResult<T> build(T body, ResultCodeEnum RResultCodeEnum) {
        RResult<T> RResult = build(body);
        RResult.setCode(RResultCodeEnum.getCode());
        RResult.setMessage(RResultCodeEnum.getMessage());
        return RResult;
    }

    public static<T> RResult<T> ok(){
        return RResult.ok(null);
    }

    /**
     * 操作成功
     * @param data
     * @param <T>
     * @return
     */
    public static<T> RResult<T> ok(T data){
        RResult<T> RResult = build(data);
        return build(data, ResultCodeEnum.SUCCESS);
    }

    public static<T> RResult<T> fail(){
        return RResult.fail(null);
    }

    /**
     * 操作失败
     * @param data
     * @param <T>
     * @return
     */
    public static<T> RResult<T> fail(T data){
        RResult<T> RResult = build(data);
        return build(data, ResultCodeEnum.FAIL);
    }

    public RResult<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    public RResult<T> code(Integer code){
        this.setCode(code);
        return this;
    }


    public boolean isOk() {
        if(this.getCode().intValue() == ResultCodeEnum.SUCCESS.getCode().intValue()) {
            return true;
        }
        return false;
    }
}


